package com.mc.web.controller.system;

import java.util.List;

import com.mc.common.core.domain.model.LoginUser;
import com.mc.common.utils.DateUtils;
import com.mc.common.utils.ServletUtils;
import com.mc.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mc.common.annotation.Log;
import com.mc.common.core.controller.BaseController;
import com.mc.common.core.domain.AjaxResult;
import com.mc.common.enums.BusinessType;
import com.mc.system.domain.SysAppVersion;
import com.mc.system.service.ISysAppVersionService;
import com.mc.common.utils.poi.ExcelUtil;
import com.mc.common.core.page.TableDataInfo;

/**
 * app版本信息 Controller
 *
 * @author mc
 * @date 2021-04-14
 */
@RestController
@RequestMapping("/system/version")
public class SysAppVersionController extends BaseController
{
    @Autowired
    private ISysAppVersionService sysAppVersionService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询app版本信息 列表
     */
    @PreAuthorize("@ss.hasPermi('system:version:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysAppVersion sysAppVersion)
    {
        startPage();
        List<SysAppVersion> list = sysAppVersionService.selectSysAppVersionList(sysAppVersion);
        return getDataTable(list);
    }

    /**
     * 导出app版本信息 列表
     */
    @PreAuthorize("@ss.hasPermi('system:version:export')")
    @Log(title = "app版本信息 ", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysAppVersion sysAppVersion)
    {
        List<SysAppVersion> list = sysAppVersionService.selectSysAppVersionList(sysAppVersion);
        ExcelUtil<SysAppVersion> util = new ExcelUtil<SysAppVersion>(SysAppVersion.class);
        return util.exportExcel(list, "version");
    }

    /**
     * 获取app版本信息 详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:version:query')")
    @GetMapping(value = "/{appId}")
    public AjaxResult getInfo(@PathVariable("appId") String appId)
    {
        return AjaxResult.success(sysAppVersionService.selectSysAppVersionById(appId));
    }

    /**
     * 新增app版本信息
     */
    @PreAuthorize("@ss.hasPermi('system:version:add')")
    @Log(title = "app版本信息 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysAppVersion sysAppVersion)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        sysAppVersion.setCreatedBy(loginUser.getUsername());
        sysAppVersion.setCreatedTime(DateUtils.getNowDate());
        return toAjax(sysAppVersionService.insertSysAppVersion(sysAppVersion));
    }

    /**
     * 修改app版本信息
     */
    @PreAuthorize("@ss.hasPermi('system:version:edit')")
    @Log(title = "app版本信息 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAppVersion sysAppVersion)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        sysAppVersion.setUpdatedBy(loginUser.getUsername());
        sysAppVersion.setUpdatedTime(DateUtils.getNowDate());
        return toAjax(sysAppVersionService.updateSysAppVersion(sysAppVersion));
    }
    /**
     * 删除app版本信息
     */
    @PreAuthorize("@ss.hasPermi('system:version:remove')")
    @Log(title = "app版本信息 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{appIds}")
    public AjaxResult remove(@PathVariable String[] appIds)
    {
        return toAjax(sysAppVersionService.deleteSysAppVersionByIds(appIds));
    }

}
